﻿namespace CodeParser.Tokens
{
    public class CharBlock
        : TokenParser
    {

        public CharBlock(TokenType type, char begin, char end, char escape, char notAllowed)
            : base(type)
        {
            BeginChar = begin;
            EndChar = end;
            EscapeChar = escape;
            NotAllowed = notAllowed;
        }

        public override Token GetTokenAt(string source, int index)
        {
            if (source[index] == BeginChar)
            {
                var lastChar = BeginChar;
                var sourceLength = source.Length;
                for (var i = index + 1; i < sourceLength; i++)
                {
                    var currentChar = source[i];
                    if (currentChar == NotAllowed) return null;
                    if (currentChar == EndChar && lastChar != EscapeChar)
                    {
                        return new Token() { Index = index, Type = Type.Value, Value = source.Substring(index, i - index + 1) };
                    }
                    lastChar = currentChar;
                }
            }
            return null;
        }

        public char BeginChar;
        public char EscapeChar;
        public char EndChar;
        public char NotAllowed;

    }
}
